<?php

class UnitChange_IndexController extends Zend_Controller_Action
{
	protected $_object;

	public function init()
	{
		//tạo đối tượng
		$this->_object = new Admin_Model_UnitChange ();
		
		//lấy tiêu đề 
		$this->_helper->common->setCaptions ();
	}

	public function indexAction()
	{
		//tham so 
		$page = $this->_request->getParam ( 'page' );
		$page = $page == '' ? 1 : $page;
		$limit = $this->_request->getParam ( 'limit', 10 );
		$to_unit_change_code = $page * $limit;
		$from_unit_change_code = $to_unit_change_code - $limit;
		
		//danh sach cac don vi tinh
		$unit = new Admin_Model_Unit ();
		$units = $unit->fetchAllBase ();
		
		//lay du lieu bang gia cuoc
		$array = array ();
		//$where = "(unit_change_code >= $from_unit_change_code AND unit_change_code < $to_unit_change_code) AND unit_change_code>0";
		$where = NULL;
		$unit_changes = $this->_object->fetchAllBase ( $where, array("unit_change_code ASC","unit_change_unit_code ASC") );
		$flag = "";
		foreach ( $unit_changes as $unit_change )
		{
			if ($flag != $unit_change->unit_change_code)
			{
				$flag = $unit_change->unit_change_code;
				$array [$flag] ['unit_change_name'] = $unit_change->unit_change_name;
			}
			$array [$flag] [$unit_change->unit_change_unit_code] = $unit_change->unit_change_rate;
		}
		
		//lay muc can nang cao nhat (kg) de phan trang, mỗi trang 10 kg
		//$max_unit_change_code = $this->_object->maxBase ( "unit_change_code", null, 0 );
		//$this->_helper->common->virtualPaginate ( $max_unit_change_code, $limit );
		

		//truyen ra ve
		$this->view->headScript ()->appendFile ( JS . 'helpers.js' ); //number_format function	
		$this->view->array = $array;
		$this->view->units = $units;
	}

	public function ajaxUpdateAction()
	{
		$this->getHelper ( 'layout' )->disableLayout ( true );
		$this->getHelper ( 'viewRenderer' )->setNoRender ( true );
		$request = $this->_request;
		$unit_change_code = $request->getParam ( 'unit_change_code' );
		$unit_change_unit_code = $request->getParam ( 'unit_change_unit_code' );
		$unit_change_rate = $request->getParam ( 'unit_change_rate' );
		$where = "unit_change_code='$unit_change_code' AND unit_change_unit_code='$unit_change_unit_code'";
		$this->_object->deleteAllBase ( $where );
		$this->_object->setUnit_change_unit_code ( $unit_change_unit_code );
		$this->_object->setUnit_change_code ( $unit_change_code );
		$this->_object->setUnit_change_rate ( $unit_change_rate );
		$this->_object->insertBase ();
		
		//cap nhat cach qui đổi
		$change=new Admin_Model_Change();
		$change->updateChange($unit_change_code, $unit_change_unit_code,$unit_change_rate);
	}

	public function insertAction()
	{
		$request = $this->_request;
		$adminAction = $request->getParam ( "adminAction" );
		$form = new UnitChange_Form_UnitChange_Insert ( $this );
		if ($request->isPost ())
		{
			if ($form->isValid ( $request->getPost () ))
			{
				$object = new Admin_Model_UnitChange ( $form->getValues () );
				$where = "unit_change_code='{$object->unit_change_code}'";
				$count = $object->countBase ( $where, 0 );
				if ($count > 0)
				{
					$form->getElement ( "unit_change_code" )->addError ( $this->_helper->common->getCaption ( 'unit_change_code' ) . ' đã tồn tại.' );
					$form->markAsError ();
				}
				if (! $form->isErrors ())
				{
					try
					{
						$object->insertBase ();
						$this->_helper->flashMessenger ( 'Thêm thành công' );
						if ($adminAction == 'save-and-close') //Tro ve trang danh sach.
						{
							$this->_helper->redirector ( 'index', 'index', 'unit-change', array () );
						}
						else if ($adminAction == 'save-and-new')
						{
							$this->_helper->redirector ( 'insert', 'index', 'unit-change', array () );
						}
					}
					catch ( Exception $e )
					{
					
					}
				}
			}
		}
		$this->view->form = $form;
	}

	public function updateAction()
	{
		$request = $this->_request;
		$unit_change_code = $request->getParam ( 'unit_change_code' );
		$adminAction = $request->getParam ( "adminAction" );
		$form = new UnitChange_Form_UnitChange_Update ( $this );
		$unit_change = new Admin_Model_UnitChange ();
		$where = " unit_change_code='$unit_change_code' AND unit_change_unit_code='' ";
		$unit_change->fetchRowAllBase ( $where );
		if ($request->isPost ())
		{
			if ($form->isValid ( $request->getPost () ))
			{
				try
				{
					$object = new Admin_Model_UnitChange ();
					$object->setUnit_change_name ( $form->getValue ( "unit_change_name" ) );
					$object->updateAllBase ( $where );
					$this->_helper->flashMessenger ( 'Cập nhật thành công' );
					if ($adminAction == 'save-and-close')
					{
						$this->_helper->redirector ( 'index', 'index', 'unit-change', array () );
					}
					else if ($adminAction == 'save-and-new')
					{
						$this->_helper->redirector ( 'insert', 'index', 'unit-change', array () );
					}
				}
				catch ( Exception $e )
				{
				
				}
			}
		}
		else
		{
			$form->populate ( $unit_change->toArray () );
		}
		$this->view->form = $form;
	}

	public function deleteAction()
	{
		$request = $this->_request;
		$unit_change_code = $request->getParam ( 'id' );
		$str_unit_change_code = implode ( "','", $unit_change_code );
		$where = "unit_change_code IN ('$str_unit_change_code')";
		if ($this->beforeDelete ())
		{
			$this->_object->deleteAllBase ( $where );
			$this->afterDelete ();
			$this->_helper->flashMessenger ( "Xóa thành công" );
		}
		$this->_helper->redirector ( 'index', 'index', 'unit-change', array () );
	}

	//hàm kiểm tra dữ liệu ràng buộc trước khi xóa
	//nếu trả về true thì cho xóa, ngược lại kg cho xóa
	public function beforeDelete()
	{
		$request = Zend_Controller_Front::getInstance ()->getRequest ();
		$unit_change_codes = ( array ) $request->getParam ( 'id' );
		$msg = "";
		foreach ( $unit_change_codes as $unit_change_code )
		{
			//lay bo don vi tinh
			$unit_change = new Admin_Model_UnitChange ();
			$unit_change->unit_change_code = $unit_change_code;
			$unit_change->fetchRowBase ();
			
			//Kiem tra xem co nam trong bang san pham tbl_product...
			$product = new Admin_Model_Product ();
			if ($product->fetchRowAllBase ( "product_unit_change_code = '{$unit_change->unit_change_code}' " ) !== FALSE)
			{
				$msg .= " - Sản phẩm có mã số: <strong>{$product->product_code}</strong>";
				break;
			}
		}
		if ($msg != "")
		{
			$msg = "Dữ liệu này đã có trong:<br />" . $msg;
			$this->_helper->flashMessenger ( $msg );
			return false;
		}
		return true;
	}

	//hàm giải phóng dữ liệu liên quan sau khi xóa
	public function afterDelete()
	{
	
	}
}